package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.MigrateDictRelation;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典迁移映射关系类-[表名: 字典迁移映射关系(migrate_dict_relation)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateDictRelationDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateDictRelationDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateDictRelation(MigrateDictRelation migrateDictRelation) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_dict_relation ( "
                + "relation_id,source_table_id,source_table_code,source_table_name,source_field_code,source_field_name,target_table_id,target_table_code,target_table_name,target_field_code,target_field_name,dict_id,dict_code,dict_name"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateDictRelation.getRelationId());
            ps.setObject(2, migrateDictRelation.getSourceTableId());
            ps.setObject(3, migrateDictRelation.getSourceTableCode());
            ps.setObject(4, migrateDictRelation.getSourceTableName());
            ps.setObject(5, migrateDictRelation.getSourceFieldCode());
            ps.setObject(6, migrateDictRelation.getSourceFieldName());
            ps.setObject(7, migrateDictRelation.getTargetTableId());
            ps.setObject(8, migrateDictRelation.getTargetTableCode());
            ps.setObject(9, migrateDictRelation.getTargetTableName());
            ps.setObject(10, migrateDictRelation.getTargetFieldCode());
            ps.setObject(11, migrateDictRelation.getTargetFieldName());
            ps.setObject(12, migrateDictRelation.getDictId());
            ps.setObject(13, migrateDictRelation.getDictCode());
            ps.setObject(14, migrateDictRelation.getDictName());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateDictRelation is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateDictRelation migrateDictRelation) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_dict_relation where 1=1"
                + " AND relation_id = ? "
                );
            ps.setObject(1, migrateDictRelation.getRelationId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateDictRelation is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateDictRelation migrateDictRelation) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_dict_relation set "
                + " relation_id = ? ,"
                + " source_table_id = ? ,"
                + " source_table_code = ? ,"
                + " source_table_name = ? ,"
                + " source_field_code = ? ,"
                + " source_field_name = ? ,"
                + " target_table_id = ? ,"
                + " target_table_code = ? ,"
                + " target_table_name = ? ,"
                + " target_field_code = ? ,"
                + " target_field_name = ? ,"
                + " dict_id = ? ,"
                + " dict_code = ? ,"
                + " dict_name = ? "
                + " where 1=1"
                + " AND relation_id = ? "
                );
            ps.setObject(1, migrateDictRelation.getRelationId());
            ps.setObject(2, migrateDictRelation.getSourceTableId());
            ps.setObject(3, migrateDictRelation.getSourceTableCode());
            ps.setObject(4, migrateDictRelation.getSourceTableName());
            ps.setObject(5, migrateDictRelation.getSourceFieldCode());
            ps.setObject(6, migrateDictRelation.getSourceFieldName());
            ps.setObject(7, migrateDictRelation.getTargetTableId());
            ps.setObject(8, migrateDictRelation.getTargetTableCode());
            ps.setObject(9, migrateDictRelation.getTargetTableName());
            ps.setObject(10, migrateDictRelation.getTargetFieldCode());
            ps.setObject(11, migrateDictRelation.getTargetFieldName());
            ps.setObject(12, migrateDictRelation.getDictId());
            ps.setObject(13, migrateDictRelation.getDictCode());
            ps.setObject(14, migrateDictRelation.getDictName());
            ps.setObject(15, migrateDictRelation.getRelationId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictRelation is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateDictRelation queryByPk(MigrateDictRelation migrateDictRelation) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateDictRelation result = null;
        try {
            ps = conn.prepareStatement("select relation_id,source_table_id,source_table_code,source_table_name,source_field_code,source_field_name,target_table_id,target_table_code,target_table_name,target_field_code,target_field_name,dict_id,dict_code,dict_namefrom migrate_dict_relation where 1=1 "
                + " AND relation_id = ? "
                );
            ps.setObject(1, migrateDictRelation.getRelationId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateDictRelation();
				result.setRelationId(rs.getString("relation_id"));
				result.setSourceTableId(rs.getString("source_table_id"));
				result.setSourceTableCode(rs.getString("source_table_code"));
				result.setSourceTableName(rs.getString("source_table_name"));
				result.setSourceFieldCode(rs.getString("source_field_code"));
				result.setSourceFieldName(rs.getString("source_field_name"));
				result.setTargetTableId(rs.getString("target_table_id"));
				result.setTargetTableCode(rs.getString("target_table_code"));
				result.setTargetTableName(rs.getString("target_table_name"));
				result.setTargetFieldCode(rs.getString("target_field_code"));
				result.setTargetFieldName(rs.getString("target_field_name"));
				result.setDictId(rs.getString("dict_id"));
				result.setDictCode(rs.getString("dict_code"));
				result.setDictName(rs.getString("dict_name"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictRelation is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateDictRelation 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateDictRelation> queryAll(MigrateDictRelation migrateDictRelation) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateDictRelation> result = new ArrayList<>();
        try {
            String sql = "select relation_id,source_table_id,source_table_code,source_table_name,source_field_code,source_field_name,target_table_id,target_table_code,target_table_name,target_field_code,target_field_name,dict_id,dict_code,dict_namefrom migrate_dict_relation where 1=1 ";
                if (migrateDictRelation.getRelationId() != null) {
                    sql = sql + " AND relation_id =  '" + migrateDictRelation.getRelationId() + "'";
                }
                if (migrateDictRelation.getSourceTableId() != null) {
                    sql = sql + " AND source_table_id =  '" + migrateDictRelation.getSourceTableId() + "'";
                }
                if (migrateDictRelation.getSourceTableCode() != null) {
                    sql = sql + " AND source_table_code =  '" + migrateDictRelation.getSourceTableCode() + "'";
                }
                if (migrateDictRelation.getSourceTableName() != null) {
                    sql = sql + " AND source_table_name =  '" + migrateDictRelation.getSourceTableName() + "'";
                }
                if (migrateDictRelation.getSourceFieldCode() != null) {
                    sql = sql + " AND source_field_code =  '" + migrateDictRelation.getSourceFieldCode() + "'";
                }
                if (migrateDictRelation.getSourceFieldName() != null) {
                    sql = sql + " AND source_field_name =  '" + migrateDictRelation.getSourceFieldName() + "'";
                }
                if (migrateDictRelation.getTargetTableId() != null) {
                    sql = sql + " AND target_table_id =  '" + migrateDictRelation.getTargetTableId() + "'";
                }
                if (migrateDictRelation.getTargetTableCode() != null) {
                    sql = sql + " AND target_table_code =  '" + migrateDictRelation.getTargetTableCode() + "'";
                }
                if (migrateDictRelation.getTargetTableName() != null) {
                    sql = sql + " AND target_table_name =  '" + migrateDictRelation.getTargetTableName() + "'";
                }
                if (migrateDictRelation.getTargetFieldCode() != null) {
                    sql = sql + " AND target_field_code =  '" + migrateDictRelation.getTargetFieldCode() + "'";
                }
                if (migrateDictRelation.getTargetFieldName() != null) {
                    sql = sql + " AND target_field_name =  '" + migrateDictRelation.getTargetFieldName() + "'";
                }
                if (migrateDictRelation.getDictId() != null) {
                    sql = sql + " AND dict_id =  '" + migrateDictRelation.getDictId() + "'";
                }
                if (migrateDictRelation.getDictCode() != null) {
                    sql = sql + " AND dict_code =  '" + migrateDictRelation.getDictCode() + "'";
                }
                if (migrateDictRelation.getDictName() != null) {
                    sql = sql + " AND dict_name =  '" + migrateDictRelation.getDictName() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateDictRelation obj = new MigrateDictRelation();
				obj.setRelationId(rs.getString("relation_id"));
				obj.setSourceTableId(rs.getString("source_table_id"));
				obj.setSourceTableCode(rs.getString("source_table_code"));
				obj.setSourceTableName(rs.getString("source_table_name"));
				obj.setSourceFieldCode(rs.getString("source_field_code"));
				obj.setSourceFieldName(rs.getString("source_field_name"));
				obj.setTargetTableId(rs.getString("target_table_id"));
				obj.setTargetTableCode(rs.getString("target_table_code"));
				obj.setTargetTableName(rs.getString("target_table_name"));
				obj.setTargetFieldCode(rs.getString("target_field_code"));
				obj.setTargetFieldName(rs.getString("target_field_name"));
				obj.setDictId(rs.getString("dict_id"));
				obj.setDictCode(rs.getString("dict_code"));
				obj.setDictName(rs.getString("dict_name"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateDictRelation is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
